<?
class ntp extends module {

    // Author: Herman Tolentino MD
    // CHITS Project 2004

    function ntp() {
        //
        // do not forget to update version
        //
        $this->author = 'Herman Tolentino MD';
        $this->version = "0.2-".date("Y-m-d");
        $this->module = "ntp";
        $this->description = "CHITS Module - Natl TB Program";

    }

    // --------------- STANDARD MODULE FUNCTIONS ------------------

    function init_deps() {
    //
    // insert dependencies in module_dependencies
    //
        module::set_dep($this->module, "module");
        module::set_dep($this->module, "healthcenter");
        module::set_dep($this->module, "complaint");
        module::set_dep($this->module, "lab");

    }

    function init_lang() {
    //
    // insert necessary language directives
    //
        module::set_lang("FTITLE_PATIENT_TYPE_FORM", "english", "PATIENT TYPE FORM", "Y");
        module::set_lang("LBL_TYPE_ID", "english", "TYPE ID", "Y");
        module::set_lang("LBL_TYPE_NAME", "english", "TYPE NAME", "Y");
        module::set_lang("FTITLE_NTP_PATIENT_TYPE", "english", "NTP PATIENT TYPES", "Y");
        module::set_lang("THEAD_ID", "english", "ID", "Y");
        module::set_lang("THEAD_NAME", "english", "NAME", "Y");
        module::set_lang("FTITLE_TREATMENT_OUTCOME_FORM", "english", "TREATMENT OUTCOME FORM", "Y");
        module::set_lang("FTITLE_NTP_TREATMENT_OUTCOME", "english", "TREATMENT OUTCOMES", "Y");
        module::set_lang("LBL_OUTCOME_ID", "english", "OUTCOME ID", "Y");
        module::set_lang("LBL_OUTCOME_NAME", "english", "OUTCOME NAME", "Y");
        module::set_lang("FTITLE_NTP_TREATMENT_PARTNER", "english", "TREATMENT PARTNERS", "Y");
        module::set_lang("FTITLE_TREATMENT_PARTNER_FORM", "english", "TREATMENT PARTNER FORM", "Y");
        module::set_lang("LBL_PARTNER_ID", "english", "PARTNER ID", "Y");
        module::set_lang("LBL_PARTNER_NAME", "english", "PARTNER NAME", "Y");
        module::set_lang("LBL_SPUTUM_EXAM", "english", "SPUTUM EXAM DONE", "Y");
        module::set_lang("LBL_PATIENT_TYPE", "english", "PATIENT TYPE", "Y");
        module::set_lang("LBL_TREATMENT_CAT", "english", "TREATMENT CATEGORY", "Y");
        module::set_lang("LBL_TREATMENT_PARTNER", "english", "TREATMENT PARTNER", "Y");
        module::set_lang("FTITLE_NTP_DATA_FORM", "english", "NTP CONSULT DATA", "Y");
        module::set_lang("FTITLE_NTP_LAB_FORM", "english", "NTP LAB REQUEST", "Y");
        module::set_lang("LBL_LAB_EXAM", "english", "LAB EXAM", "Y");
        module::set_lang("FTITLE_NTP_LABS", "english", "NTP LAB EXAMS", "Y");
        module::set_lang("LBL_LABS", "english", "LAB EXAMS", "Y");
        module::set_lang("LBL_TREATMENT_OUTCOME", "english", "TREATMENT OUTCOME", "Y");

    }

    function init_stats() {
    }

    function init_help() {
    }

    function init_menu() {
        // use this for updating menu system
        if (func_num_args()>0) {
            $arg_list = func_get_args();
        }

        // menu entries
        module::set_menu($this->module, "NTP Stats", "STATS", "_ntp_stats");
        module::set_menu($this->module, "NTP TCL", "REPORTS", "_ntp_report");
        module::set_menu($this->module, "NTP Patient Type", "LIBRARIES", "_ntp_patient_type");
        module::set_menu($this->module, "NTP Outcomes", "LIBRARIES", "_ntp_outcomes");
        module::set_menu($this->module, "NTP Partners", "LIBRARIES", "_ntp_partners");
        module::set_menu($this->module, "NTP Lab Exams", "LIBRARIES", "_ntp_labs");

        // add more detail
        module::set_detail($this->description, $this->version, $this->author, $this->module);

    }

    function init_sql() {
        if (func_num_args()>0) {
            $arg_list = func_get_args();
        }

        module::execsql("CREATE TABLE `m_lib_ntp_patient_type` (".
            "`type_id` varchar(10) NOT NULL default '',".
            "`type_name` varchar(40) NOT NULL default '',".
            "PRIMARY KEY  (`type_id`)".
            ") TYPE=InnoDB; ");

        // load initial data
        module::execsql("insert into m_lib_ntp_patient_type (type_id, type_name) values ('NEW', 'New Patient')");
        module::execsql("insert into m_lib_ntp_patient_type (type_id, type_name) values ('REL', 'Relapse')");
        module::execsql("insert into m_lib_ntp_patient_type (type_id, type_name) values ('TIN', 'Transfer In')");
        module::execsql("insert into m_lib_ntp_patient_type (type_id, type_name) values ('TLOSS', 'Transfer After Loss')");
        module::execsql("insert into m_lib_ntp_patient_type (type_id, type_name) values ('FAIL', 'Treatment Failure')");
        module::execsql("insert into m_lib_ntp_patient_type (type_id, type_name) values ('OTH', 'Others')");

        module::execsql("CREATE TABLE `m_lib_ntp_treatment_outcome` (".
            "`outcome_id` varchar(10) NOT NULL default '',".
            "`outcome_name` varchar(40) NOT NULL default '',".
            "PRIMARY KEY  (`outcome_id`)".
            ") TYPE=InnoDB;");

        // load initial data
        module::execsql("insert into m_lib_ntp_treatment_outcome (outcome_id, outcome_name) values ('CURE', 'Cured')");
        module::execsql("insert into m_lib_ntp_treatment_outcome (outcome_id, outcome_name) values ('COMP', 'Treatment Completed')");
        module::execsql("insert into m_lib_ntp_treatment_outcome (outcome_id, outcome_name) values ('DIED', 'Patient Died')");
        module::execsql("insert into m_lib_ntp_treatment_outcome (outcome_id, outcome_name) values ('FAIL', 'Treatment Failure')");
        module::execsql("insert into m_lib_ntp_treatment_outcome (outcome_id, outcome_name) values ('LOST', 'Lost to Follow-up')");
        module::execsql("insert into m_lib_ntp_treatment_outcome (outcome_id, outcome_name) values ('TOUT', 'Transfer Out')");
        module::execsql("insert into m_lib_ntp_treatment_outcome (outcome_id, outcome_name) values ('TX', 'Under Treatment')");

        // visit_type: I initial F followup
        module::execsql("CREATE TABLE `m_consult_ntp` (".
            "`consult_id` float NOT NULL default '0',".
            "`ntp_id` float NOT NULL default '0',".
            "`ntp_timestamp` timestamp(14) NOT NULL,".
            "`user_id` float NOT NULL default '0',".
            "`treatment_month` int(11) NOT NULL default '0',".
            "`sputum_exam` char(1) NOT NULL default 'Y',".
            "`visit_type` varchar(100) NOT NULL default '',".
            "`patient_type` varchar(10) NOT NULL default '',".
            "`treatment_outcome` varchar(10) NOT NULL default '',".
            "`treatment_partner` varchar(10) NOT NULL default '',".
            "`treatment_cat` varchar(10) NOT NULL default '',".
            "`healthcenter_id` varchar(10) NOT NULL default '',".
            "PRIMARY KEY  (`ntp_id`,`consult_id`)".
            ") TYPE=InnoDB;");

        module::execsql("CREATE TABLE `m_patient_ntp` (".
            "`patient_id` float NOT NULL default '0',".
            "`ntp_id` float NOT NULL auto_increment,".
            "`registration_date` date NOT NULL default '0000-00-00',".
            "PRIMARY KEY  (`ntp_id`)".
            ") TYPE=InnoDB; ");

        module::execsql("CREATE TABLE `m_consult_ntp_sputum` (".
            "`consult_id` float NOT NULL default '0',".
            "`sputum_exam_date` date NOT NULL default '0000-00-00',".
            "`sputum_exam_result` varchar(10) NOT NULL default '',".
            "PRIMARY KEY  (`consult_id`)".
            ") TYPE=InnoDB;");

        module::execsql("CREATE TABLE `m_lib_ntp_treatment_partner` (".
            "`partner_id` varchar(10) NOT NULL default '',".
            "`partner_name` varchar(50) NOT NULL default '',".
            "PRIMARY KEY  (`partner_id`)".
            ") TYPE=InnoDB; ");

        // load initial data
        module::execsql("insert into m_lib_ntp_treatment_partner (partner_id, partner_name) values ('PRIVMD', 'Private Physician')");
        module::execsql("insert into m_lib_ntp_treatment_partner (partner_id, partner_name) values ('HCMD', 'Health Center Physician')");
        module::execsql("insert into m_lib_ntp_treatment_partner (partner_id, partner_name) values ('BHW', 'Barangay Health Worker')");
        module::execsql("insert into m_lib_ntp_treatment_partner (partner_id, partner_name) values ('BN', 'Barangay Nurse')");
        module::execsql("insert into m_lib_ntp_treatment_partner (partner_id, partner_name) values ('BMW', 'Barangay Midwife')");
        module::execsql("insert into m_lib_ntp_treatment_partner (partner_id, partner_name) values ('FAM', 'Family Member')");

        // NTP Labs
        module::execsql("CREATE TABLE `m_consult_ntp_labs` (".
            "`lab_id` varchar(10) NOT NULL default '',".
            "PRIMARY KEY  (`lab_id`)".
            ") TYPE=InnoDB;");

        // load initial data
        module::execsql("insert into m_consult_ntp_labs (lab_id) values ('CXR')");
        module::execsql("insert into m_consult_ntp_labs (lab_id) values ('SPT')");

    }

    function drop_tables() {

        module::execsql("DROP TABLE `m_lib_ntp_patient_type`;");
        module::execsql("DROP TABLE `m_lib_ntp_treatment_outcome`;");
        module::execsql("DROP TABLE `m_consult_ntp`;");
        module::execsql("DROP TABLE `m_consult_ntp_sputum`;");
        module::execsql("DROP TABLE `m_lib_ntp_treatment_partner`;");
        module::execsql("DROP TABLE `m_consult_ntp_labs`;");
    }


    // --------------- CUSTOM MODULE FUNCTIONS ------------------

    function _ntp_labs() {
    //
    // main submodule for segregating NTP Labs
    // calls form_ntp_lab()
    //       display_ntp_lab()
    //       process_ntp_lab()
    //
        // always check dependencies
        if ($exitinfo = $this->missing_dependencies('ntp')) {
            return print($exitinfo);
        }
        if (func_num_args()>0) {
            $arg_list = func_get_args();
            $menu_id = $arg_list[0];
            $post_vars = $arg_list[1];
            $get_vars = $arg_list[2];
            $validuser = $arg_list[3];
            $isadmin = $arg_list[4];
            //print_r($arg_list);
        }
        $n = new ntp;
        if ($post_vars["submitntplab"]) {
            $n->process_ntp_lab($menu_id, $post_vars, $get_vars);
        }
        $n->display_ntp_labs($menu_id, $post_vars, $get_vars);
        $n->form_ntp_lab($menu_id, $post_vars, $get_vars);
    }

    function show_ntp_labs() {
        if (func_num_args()>0) {
            $arg_list = func_get_args();
            $type_id = $arg_list[0];
        }
        $sql = "select n.lab_id, l.lab_name ".
               "from m_lib_laboratory l, m_consult_ntp_labs n ".
               "where l.lab_id = n.lab_id order by l.lab_name";
        if ($result = mysql_query($sql)) {
            if (mysql_num_rows($result)) {
                while (list($id, $name) = mysql_fetch_array($result)) {
                    print "<input type='checkbox' name='ntp_lab[]' value='$id' class='textbox' /> $name<br />";
                }
                return $ret_val;
            }
        }
    }

    function form_ntp_lab() {
    //
    // called by _ntp_patient_type()
    //
        if (func_num_args()) {
            $arg_list = func_get_args();
            $menu_id = $arg_list[0];
            $post_vars = $arg_list[1];
            $get_vars = $arg_list[2];
        }
        print "<table width='400'>";
        print "<form action = '".$_SERVER["SELF"]."?page=LIBRARIES&menu_id=$menu_id' name='form_patient_type' method='post'>";
        print "<tr valign='top'><td>";
        print "<span class='library'>".FTITLE_NTP_LAB_FORM."</span><br><br>";
        print "</td></tr>";
        print "<tr valign='top'><td>";
        print "<span class='boxtitle'>".LBL_LABS."</span><br> ";
        print lab::show_lab_exams();
        print "</td></tr>";
        print "<tr><td><br>";
        if ($get_vars["lab_id"]) {
            print "<input type='hidden' name='type_id' value='".$get_vars["type_id"]."'>";
            if ($_SESSION["priv_update"]) {
                print "<input type='submit' value = 'Update Type' class='textbox' name='submittype' style='border: 1px solid #000000'> ";
            }
            if ($_SESSION["priv_delete"]) {
                print "<input type='submit' value = 'Delete Type' class='textbox' name='submittype' style='border: 1px solid #000000'> ";
            }
        } else {
            if ($_SESSION["priv_add"]) {
                print "<input type='submit' value = 'Add NTP Exam' class='textbox' name='submitntplab' style='border: 1px solid #000000'> ";
            }
        }
        print "</td></tr>";
        print "</form>";
        print "</table><br>";

    }

    function display_ntp_labs() {
        if (func_num_args()) {
            $arg_list = func_get_args();
            $menu_id = $arg_list[0];
            $post_vars = $arg_list[1];
            $get_vars = $arg_list[2];
            $validuser = $arg_list[3];
            $isadmin = $arg_list[4];
        }
        print "<table width='400'>";
        print "<form action = '".$_SERVER["SELF"]."?page=LIBRARIES&menu_id=$menu_id' name='form_ntp_lab' method='post'>";
        print "<tr valign='top'><td>";
        print "<span class='library'>".FTITLE_NTP_LABS."</span><br>";
        print "</td></tr>";
        print $sql = "select n.lab_id, l.lab_name ".
               "from m_lib_laboratory l, m_consult_ntp_labs n ".
               "where l.lab_id = n.lab_id ".
               "order by l.lab_name";
        if ($result = mysql_query($sql)) {
            if (mysql_num_rows($result)) {
                while (list($id, $name) = mysql_fetch_array($result)) {
                    print "<tr valign='top'><td><input type='checkbox' name='ntp_labs[]' value='$id'/> $name</td></tr>";
                }
                print "<tr valign='top'><td>";
                if ($_SESSION["priv_delete"]) {
                    print "<input type='submit' value = 'Delete NTP Exam' class='textbox' name='submitntplab' style='border: 1px solid #000000'> ";
                }
                print "</td></tr>";
            }
        }
        print "</form>";
        print "</table><br>";
    }

    function process_ntp_lab() {
        if (func_num_args()>0) {
            $arg_list = func_get_args();
            $menu_id = $arg_list[0];
            $post_vars = $arg_list[1];
            $get_vars = $arg_list[2];
            $validuser = $arg_list[3];
            $isadmin = $arg_list[4];
            print_r($arg_list);
        }
        if ($post_vars["submitntplab"]) {
            switch($post_vars["submitntplab"]) {
            case "Add NTP Exam":
                if ($post_vars["lab_exam"]) {
                    $sql = "insert into m_consult_ntp_labs (lab_id) ".
                           "values ('".$post_vars["lab_exam"]."')";
                    if ($result = mysql_query($sql)) {
                        header("location: ".$_SERVER["PHP_SELF"]."?page=".$get_vars["page"]."&menu_id=$menu_id");
                    }
                }
                break;
            case "Delete NTP Exam":
                    foreach($post_vars["ntp_labs"] as $key=>$value) {
                        $sql = "delete from m_consult_ntp_labs where lab_id = '$value'";
                        $result = mysql_query($sql);
                    }
                    header("location: ".$_SERVER["PHP_SELF"]."?page=".$get_vars["page"]."&menu_id=$menu_id");
                break;
            }
        }
    }

    function _consult_ntp() {
    //
    // main submodule for consult
    // called from healthcenter _consult()
    // calls form_patient_type()
    //       display_patient_type()
    //       process_patient_type()
    //
        // always check dependencies
        if ($exitinfo = $this->missing_dependencies('ntp')) {
            return print($exitinfo);
        }
        if (func_num_args()>0) {
            $arg_list = func_get_args();
            $menu_id = $arg_list[0];
            $post_vars = $arg_list[1];
            $get_vars = $arg_list[2];
            $validuser = $arg_list[3];
            $isadmin = $arg_list[4];
            //print_r($arg_list);
        }
        $n = new ntp;
        if ($post_vars["submitntp"]) {
            $n->process_consult_ntp($menu_id, $post_vars, $get_vars, $validuser, $isadmin);
        }
        $n->form_consult_ntp($menu_id, $post_vars, $get_vars, $validuser, $isadmin);

        // lab requests: either request or generate referral
        $n->form_consult_ntp_lab($menu_id, $post_vars, $get_vars, $validuser, $isadmin);
    }

    function display_course() {
        if (func_num_args()>0) {
            $arg_list = func_get_args();
            $menu_id = $arg_list[0];
            $post_vars = $arg_list[1];
            $get_vars = $arg_list[2];
            $validuser = $arg_list[3];
            $isadmin = $arg_list[4];
            //print_r($arg_list);
        }
        $patient_id = healthcenter::get_patient_id($get_vars["consult_id"]);
        $sql = "select date_format(n.ntp_timestamp, '%a %d %b %Y, %h:%i%p'), n.consult_id, n.ntp_id, n.user_id, n.treatment_month, n.visit_type, n.sputum_exam, n.patient_type, n.treatment_outcome, n.treatment_partner, n.treatment_cat ".
               "from m_consult_ntp n, m_consult c, m_patient p ".
               "where n.consult_id = c.consult_id and c.patient_id = p.patient_id ".
               "and p.patient_id = $patient_id and n.ntp_id = '".$get_vars["ntp_id"]."'";
        if ($result = mysql_query($sql)) {
            if (mysql_num_rows($result)) {
                while (list($ts, $cid, $nid, $uid, $txmonth, $visit, $sputum, $ptype, $txoutcome, $txpartner, $txcat) = mysql_fetch_array($result)) {
                    print "<table width='250' style='border: 1px dotted black'>";
                    print "<tr><td>";
                    print "Date: <b>$ts</b><br/>";
                    print "Seen By: ".user::get_username($uid)."<br/>";
                    print "Treatment Month: $txmonth<br/>";
                    print "Sputum Exam: ".($sputum?"YES":"NO")."<br/>";
                    print "Patient Type: ".ntp::get_patient_type($ptype)."<br/>";
                    print "Tx Category: ".ntp::get_treatment_cat($txcat)."<br/>";
                    print "Tx Outcome: ".ntp::get_treatment_outcome($txoutcome)."<br/>";
                    print "<img src='../images/delete.png' border='0'/>";
                    print "</td></tr>";
                    print "</table>";
                }
            }
        }
    }

    function _details_ntp() {
        if (func_num_args()>0) {
            $arg_list = func_get_args();
            $menu_id = $arg_list[0];
            $post_vars = $arg_list[1];
            $get_vars = $arg_list[2];
            $validuser = $arg_list[3];
            $isadmin = $arg_list[4];
            //print_r($arg_list);
        }
        print "<b>NTP RECORD</b><br/>";
        $sql_ntp = "select ntp_id, patient_id, date_format(registration_date, '%a %d %b %Y') ".
                   "from m_patient_ntp order by registration_date desc";
        if ($result_ntp = mysql_query($sql_ntp)) {
            if (mysql_num_rows($result_ntp)) {
                while (list($nid, $pid, $regdate) = mysql_fetch_array($result_ntp)) {
                    print "<img src='../images/arrow_redwhite' border='0'/> COURSE NO: $nid ";
                    print "<a href='".$_SERVER["PHP_SELF"]."?page=".$get_vars["page"]."&menu_id=".$get_vars["menu_id"]."&consult_id=".$get_vars["consult_id"]."&ptmenu=".$get_vars["ptmenu"]."&module=".$get_vars["module"]."&ntp_id=$nid'>$regdate</a><br/>";
                    if ($get_vars["ntp_id"] && $get_vars["ntp_id"]==$nid) {
                        ntp::display_course($menu_id, $post_vars, $get_vars);
                    }
                }
            } else {
                print "<font color='red'>No records</font>";
            }
        }
        print "<br/>";
        print "<b>NTP LABS</b><br/>";
    }

    function form_consult_ntp() {
    //
    // get ntp data for this consult
    //
        if (func_num_args()>0) {
            $arg_list = func_get_args();
            $menu_id = $arg_list[0];
            $post_vars = $arg_list[1];
            $get_vars = $arg_list[2];
            $validuser = $arg_list[3];
            $isadmin = $arg_list[4];
            //print_r($arg_list);
        }
        print "<table width='300'>";
        print "<form action = '".$_SERVER["SELF"]."?page=".$get_vars["page"]."&menu_id=$menu_id&consult_id=".$get_vars["consult_id"]."&ptmenu=".$get_vars["ptmenu"]."&module=".$get_vars["module"]."' name='form_vitalsigns' method='post'>";
        print "<tr valign='top'><td>";
        print "<b>".FTITLE_NTP_DATA_FORM."</b><br/><br/>";
        print "</td></tr>";
        print "<tr valign='top'><td>";
        if ($get_vars["ntp_id"]) {
            print "<span class='boxtitle'>".LBL_VISIT_TYPE.": FOLLOWUP</span><br> ";
            print "<b>COURSE NO:</b> ".$get_vars["ntp_id"]."<br/><br/>";
            print "<input type='hidden' name='ntp_id' value='".$get_vars["ntp_id"]."'/>";
            print "<input type='hidden' name='visit_type' value='F'/>";
        } else {
            print "<span class='boxtitle'>".LBL_VISIT_TYPE."</span><br> ";
            print "<input type='checkbox' name='visit_type' value='I'/> First Visit ";
        }
        print "</td></tr>";
        print "<tr valign='top'><td>";
        print "<span class='boxtitle'>".LBL_SPUTUM_EXAM."</span><br> ";
        print "<input type='checkbox' name='sputum_exam_done' value='1'/> Check if sputum exam done today<br />";
        print "</td></tr>";
        print "<tr valign='top'><td>";
        print "<span class='boxtitle'>".LBL_PATIENT_TYPE."</span><br> ";
        print ntp::show_patient_type();
        print "</td></tr>";
        print "<tr valign='top'><td>";
        print "<span class='boxtitle'>".LBL_TREATMENT_CAT."</span><br> ";
        print ntp::show_treatment_cat();
        print "</td></tr>";
        print "<tr valign='top'><td>";
        print "<span class='boxtitle'>".LBL_TREATMENT_PARTNER."</span><br> ";
        print ntp::show_treatment_partners();
        print "</td></tr>";
        print "<tr valign='top'><td>";
        print "<span class='boxtitle'>".LBL_TREATMENT_OUTCOME."</span><br> ";
        print ntp::show_treatment_outcomes();
        print "</td></tr>";
        print "<tr><td>";
        if ($_SESSION["priv_add"]) {
            print "<br><input type='submit' value = 'Save NTP Data' class='textbox' name='submitntp' style='border: 1px solid #000000'><br>";
        }
        print "</td></tr>";
        print "</form>";
        print "</table><br>";

    }

    function get_month_start_ntp() {
        if (func_num_args()>0) {
            $arg_list = func_get_args();
            $ntp_id = $arg_list[0];
        }
        $sql = "select month(registration_date) from m_patient_ntp where ntp_id = '$ntp_id'";
        if ($result = mysql_query($sql)) {
            if (mysql_num_rows($result)) {
                list($month) = mysql_fetch_array($result);
                return $month;
            }
        }
    }

    function get_treatment_month() {
        if (func_num_args()>0) {
            $arg_list = func_get_args();
            $startmonth = $arg_list[0];
            $date_today= $arg_list[1];
        }
        $sql = "select month('".$date_today."') - $startmonth";
        if ($result = mysql_query($sql)) {
            if (mysql_num_rows($result)) {
                list($txmonth) = mysql_fetch_array($result);
                return $txmonth;
            }
        }
    }

    function process_consult_ntp() {
        if (func_num_args()>0) {
            $arg_list = func_get_args();
            $menu_id = $arg_list[0];
            $post_vars = $arg_list[1];
            $get_vars = $arg_list[2];
            $validuser = $arg_list[3];
            $isadmin = $arg_list[4];
            //print_r($arg_list);
        }
        switch($post_vars["submitntp"]) {
        case "Save NTP Data":
            if ($post_vars["visit_type"] && $post_vars["patient_type"] && $post_vars["treatment_cat"] && $post_vars["treatment_partner"] && $post_vars["treatment_outcome"]) {
                // first visit?
                if ($post_vars["visit_type"]=="I") {
                    $treatment_month = "1";
                    $patient_id = healthcenter::get_patient_id($get_vars["consult_id"]);
                    $sql_first = "insert into m_patient_ntp (patient_id, registration_date) ".
                                 "values ('$patient_id', sysdate())";
                    if ($result_first = mysql_query($sql_first)) {
                        $insert_id = mysql_insert_id();
                        $sql = "insert into m_consult_ntp (ntp_id, consult_id, ntp_timestamp, user_id, treatment_month, sputum_exam, visit_type, patient_type, treatment_outcome, treatment_partner, treatment_cat, healthcenter_id ) ".
                               " values ('$insert_id', '".$get_vars["consult_id"]."', sysdate(), '".$_SESSION["userid"]."', '$treatment_month', '".$post_vars["sputum_exam_done"]."', '".$post_vars["visit_type"]."', '".$post_vars["patient_type"]."', '".$post_vars["treatment_outcome"]."', '".$post_vars["treatment_partner"]."', '".$post_vars["treatment_cat"]."', '".$_SESSION["datanode"]["code"]."')";
                        if ($result = mysql_query($sql)) {
                            header("location: ".$_SERVER["PHP_SELF"]."?page=".$get_vars["page"]."&menu_id=".$get_vars["menu_id"]."&consult_id=".$get_vars["consult_id"]."&ptmenu=".$get_vars["ptmenu"]."&module=".$get_vars["module"]);
                        }
                    }
                } else {
                    $ntp_startmonth = ntp::get_month_start_ntp($post_vars["ntp_id"]);
                    $ntp_treatment_month = ntp::get_treatment_month($ntp_startmonth, date("Y-m-d"))+1;
                    $sql = "insert into m_consult_ntp (ntp_id, consult_id, ntp_timestamp, user_id, treatment_month, sputum_exam, visit_type, patient_type, treatment_outcome, treatment_partner, treatment_cat, healthcenter_id ) ".
                           " values ('".$post_vars["ntp_id"]."', '".$get_vars["consult_id"]."', sysdate(), '".$_SESSION["userid"]."', '$ntp_treatment_month', '".$post_vars["sputum_exam_done"]."', '".$post_vars["visit_type"]."', '".$post_vars["patient_type"]."', '".$post_vars["treatment_outcome"]."', '".$post_vars["treatment_partner"]."', '".$post_vars["treatment_cat"]."', '".$_SESSION["datanode"]["code"]."')";
                    if ($result = mysql_query($sql) or die(mysql_error())) {
                        header("location: ".$_SERVER["PHP_SELF"]."?page=".$get_vars["page"]."&menu_id=".$get_vars["menu_id"]."&consult_id=".$get_vars["consult_id"]."&ptmenu=".$get_vars["ptmenu"]."&module=".$get_vars["module"]);
                    }
                }
            }
            break;
        case "Send Request":
            foreach($post_vars["ntp_lab"] as $key=>$value) {
                $sql = "insert into m_consult_lab (lab_id, request_timestamp, request_user_id, consult_id) ".
                       "values ('$value', sysdate(), '".$_SESSION["userid"]."', '".$get_vars["consult_id"]."')";
                $result = mysql_query($sql);
            }
            header("location: ".$_SERVER["PHP_SELF"]."?page=".$get_vars["page"]."&menu_id=".$get_vars["menu_id"]."&consult_id=".$get_vars["consult_id"]."&ptmenu=".$get_vars["ptmenu"]."&module=".$get_vars["module"]);
            break;
        case "Print Referral":
            break;
        }
    }

    function form_consult_ntp_lab() {
    //
    // get ntp data for this consult
    //
        if (func_num_args()>0) {
            $arg_list = func_get_args();
            $menu_id = $arg_list[0];
            $post_vars = $arg_list[1];
            $get_vars = $arg_list[2];
            $validuser = $arg_list[3];
            $isadmin = $arg_list[4];
            //print_r($arg_list);
        }
        print "<table width='300'>";
        print "<form action = '".$_SERVER["SELF"]."?page=".$get_vars["page"]."&menu_id=$menu_id&consult_id=".$get_vars["consult_id"]."&ptmenu=".$get_vars["ptmenu"]."&module=".$get_vars["module"]."' name='form_vitalsigns' method='post'>";
        print "<tr valign='top'><td>";
        print "<b>".FTITLE_NTP_LAB_FORM."</b><br/><br/>";
        print "</td></tr>";
        print "<tr valign='top'><td>";
        print "<span class='boxtitle'>".LBL_LAB_EXAM."</span><br> ";
        print ntp::show_ntp_labs();
        print "</td></tr>";
        print "<tr><td><br />";
        if ($_SESSION["priv_add"]) {
            print "<input type='submit' value = 'Send Request' class='textbox' name='submitntp' style='border: 1px solid #000000'> ";
            print "<input type='submit' value = 'Print Referral' class='textbox' name='submitntp' style='border: 1px solid #000000'> ";
        }
        print "<br /></td></tr>";
        print "</form>";
        print "</table><br>";

    }

    function show_treatment_cat() {
        if (func_num_args()>0) {
            $arg_list = func_get_args();
            $cat_id = $arg_list[0];
        }
        $ret_val .= "<select name='treatment_cat' class='textbox'>";
        $ret_val .= "<option value=''>Select Category</option>";
        $ret_val .= "<option value='1'>Category 1</option>";
        $ret_val .= "<option value='2'>Category 2</option>";
        $ret_val .= "<option value='3'>Category 3</option>";
        $ret_val .= "</select>";
        return $ret_val;
    }

    function get_treatment_cat() {
        if (func_num_args()>0) {
            $arg_list = func_get_args();
            $cat_id = $arg_list[0];
        }
        switch($cat_id) {
        case "1":
            return "Category 1";
            break;
        case "2":
            return "Category 2";
            break;
        case "3":
            return "Category 3";
            break;
        default:
            return "Unclassified";
        }
    }

    function _ntp_patient_type() {
    //
    // main submodule for patient type
    // calls form_patient_type()
    //       display_patient_type()
    //       process_patient_type()
    //
        // always check dependencies
        if ($exitinfo = $this->missing_dependencies('ntp')) {
            return print($exitinfo);
        }
        if (func_num_args()>0) {
            $arg_list = func_get_args();
            $menu_id = $arg_list[0];
            $post_vars = $arg_list[1];
            $get_vars = $arg_list[2];
            $validuser = $arg_list[3];
            $isadmin = $arg_list[4];
            //print_r($arg_list);
        }
        $n = new ntp;
        if ($post_vars["submittype"]) {
            $n->process_patient_type($menu_id, $post_vars, $get_vars);
        }
        $n->display_patient_type($menu_id, $post_vars, $get_vars);
        $n->form_patient_type($menu_id, $post_vars, $get_vars);
    }

    function show_patient_type() {
        if (func_num_args()>0) {
            $arg_list = func_get_args();
            $type_id = $arg_list[0];
        }
        $sql = "select type_id, type_name from m_lib_ntp_patient_type order by type_name";
        if ($result = mysql_query($sql)) {
            if (mysql_num_rows($result)) {
                $ret_val .= "<select name='patient_type' class='textbox'>";
                $ret_val .= "<option value=''>Select Type</option>";
                while (list($id, $name) = mysql_fetch_array($result)) {
                    $ret_val .= "<option value='$id'>$name</option>";
                }
                $ret_val .= "</select>";
                return $ret_val;
            }
        }
    }

    function get_patient_type() {
        if (func_num_args()>0) {
            $arg_list = func_get_args();
            $type_id = $arg_list[0];
        }
        $sql = "select type_name from m_lib_ntp_patient_type where type_id = '$type_id'";
        if ($result = mysql_query($sql)) {
            if (mysql_num_rows($result)) {
                list($name) = mysql_fetch_array($result);
                return $name;
            }
        }
    }

    function form_patient_type() {
    //
    // called by _ntp_patient_type()
    //
        if (func_num_args()) {
            $arg_list = func_get_args();
            $menu_id = $arg_list[0];
            $post_vars = $arg_list[1];
            $get_vars = $arg_list[2];
            if ($get_vars["type_id"]) {
                $sql = "select type_id, type_name ".
                       "from m_lib_ntp_patient_type where type_id = '".$get_vars["type_id"]."'";
                if ($result = mysql_query($sql)) {
                    if (mysql_num_rows($result)) {
                        $type = mysql_fetch_array($result);
                    }
                }
            }
        }
        print "<table width='400'>";
        print "<form action = '".$_SERVER["SELF"]."?page=LIBRARIES&menu_id=$menu_id' name='form_patient_type' method='post'>";
        print "<tr valign='top'><td>";
        print "<span class='library'>".FTITLE_PATIENT_TYPE_FORM."</span><br><br>";
        print "</td></tr>";
        print "<tr valign='top'><td>";
        print "<span class='boxtitle'>".LBL_TYPE_ID."</span><br> ";
        if ($get_vars["type_id"]) {
            $disable = "disabled";
            print "<input type='hidden' name='type_id' value='".$type["type_id"]."'>";
        } else {
            $disable = "";
        }
        print "<input type='text' class='textbox' size='10' $disable maxlength='10' name='type_id' value='".($type["type_id"]?$type["type_id"]:$post_vars["type_id"])."' style='border: 1px solid #000000'><br>";
        print "</td></tr>";
        print "<tr valign='top'><td>";
        print "<span class='boxtitle'>".LBL_TYPE_NAME."</span><br> ";
        print "<input type='text' class='textbox' size='15' maxlength='25' name='type_name' value='".($type["type_name"]?$type["type_name"]:$post_vars["type_name"])."' style='border: 1px solid #000000'><br>";
        print "</td></tr>";
        print "<tr><td><br>";
        if ($get_vars["type_id"]) {
            print "<input type='hidden' name='type_id' value='".$get_vars["type_id"]."'>";
            if ($_SESSION["priv_update"]) {
                print "<input type='submit' value = 'Update Type' class='textbox' name='submittype' style='border: 1px solid #000000'> ";
            }
            if ($_SESSION["priv_delete"]) {
                print "<input type='submit' value = 'Delete Type' class='textbox' name='submittype' style='border: 1px solid #000000'> ";
            }
        } else {
            if ($_SESSION["priv_add"]) {
                print "<input type='submit' value = 'Add Type' class='textbox' name='submittype' style='border: 1px solid #000000'> ";
            }
        }
        print "</td></tr>";
        print "</form>";
        print "</table><br>";

    }

    function process_patient_type() {
        if (func_num_args()>0) {
            $arg_list = func_get_args();
            $menu_id = $arg_list[0];
            $post_vars = $arg_list[1];
            $get_vars = $arg_list[2];
            $validuser = $arg_list[3];
            $isadmin = $arg_list[4];
            print_r($arg_list);
        }
        if ($post_vars["submittype"]) {
            if ($post_vars["type_id"] && $post_vars["type_name"]) {
                switch($post_vars["submittype"]) {
                case "Add Type":
                    $sql = "insert into m_lib_ntp_patient_type (type_id, type_name) ".
                           "values ('".strtoupper($post_vars["type_id"])."', '".$post_vars["type_name"]."')";
                    if ($result = mysql_query($sql)) {
                        header("location: ".$_SERVER["PHP_SELF"]."?page=".$get_vars["page"]."&menu_id=$menu_id");
                    }
                    break;
                case "Update Type":
                    $sql = "update m_lib_ntp_patient_type set ".
                           "type_name = '".$post_vars["type_name"]."' ".
                           "where type_id = '".$post_vars["type_id"]."'";
                    if ($result = mysql_query($sql)) {
                        header("location: ".$_SERVER["PHP_SELF"]."?page=".$get_vars["page"]."&menu_id=$menu_id");
                    }
                    break;
                case "Delete Type":
                    if (module::confirm_delete($menu_id, $post_vars, $get_vars)) {
                        $sql = "delete from m_lib_ntp_patient_type where type_id = '".$post_vars["type_id"]."'";
                        if ($result = mysql_query($sql)) {
                            header("location: ".$_SERVER["PHP_SELF"]."?page=".$get_vars["page"]."&menu_id=$menu_id");
                        }
                    } else {
                        if ($post_vars["confirm_delete"]=="No") {
                            header("location: ".$_SERVER["PHP_SELF"]."?page=".$get_vars["page"]."&menu_id=".$get_vars["menu_id"]);
                        }
                    }
                    break;
                }
            }
        }
    }

    function display_patient_type() {
        if (func_num_args()) {
            $arg_list = func_get_args();
            $menu_id = $arg_list[0];
            $post_vars = $arg_list[1];
            $get_vars = $arg_list[2];
            $validuser = $arg_list[3];
            $isadmin = $arg_list[4];
        }
        print "<table width='400'>";
        print "<tr valign='top'><td colspan='3'>";
        print "<span class='library'>".FTITLE_NTP_PATIENT_TYPE."</span><br>";
        print "</td></tr>";
        print "<tr valign='top'><td><b>".THEAD_ID."</b></td><td><b>".THEAD_NAME."</b></td></tr>";
        $sql = "select type_id, type_name from m_lib_ntp_patient_type order by type_name";
        if ($result = mysql_query($sql)) {
            if (mysql_num_rows($result)) {
                while (list($id, $name) = mysql_fetch_array($result)) {
                    print "<tr valign='top'><td>$id</td><td><a href='".$_SERVER["SELF"]."?page=".$get_vars["page"]."&menu_id=$menu_id&type_id=$id'>$name</a></td></tr>";
                }
            }
        }
        print "</table><br>";
    }

    function _ntp_outcomes() {
    //
    // main submodule for patient outcomes
    // calls form_outcome()
    //       display_outcome()
    //       process_outcome()
    //
        // always check dependencies
        if ($exitinfo = $this->missing_dependencies('ntp')) {
            return print($exitinfo);
        }
        if (func_num_args()>0) {
            $arg_list = func_get_args();
            $menu_id = $arg_list[0];
            $post_vars = $arg_list[1];
            $get_vars = $arg_list[2];
            $validuser = $arg_list[3];
            $isadmin = $arg_list[4];
            //print_r($arg_list);
        }
        $n = new ntp;
        if ($post_vars["submitoutcome"]) {
            $n->process_outcome($menu_id, $post_vars, $get_vars);
        }
        $n->display_outcome($menu_id, $post_vars, $get_vars);
        $n->form_outcome($menu_id, $post_vars, $get_vars);
    }

    function show_treatment_outcomes() {
        if (func_num_args()>0) {
            $arg_list = func_get_args();
            $type_id = $arg_list[0];
        }
        $sql = "select outcome_id, outcome_name from m_lib_ntp_treatment_outcome order by outcome_name";
        if ($result = mysql_query($sql)) {
            if (mysql_num_rows($result)) {
                $ret_val .= "<select name='treatment_outcome' class='textbox'>";
                $ret_val .= "<option value=''>Select Outcome</option>";
                while (list($id, $name) = mysql_fetch_array($result)) {
                    $ret_val .= "<option value='$id'>$name</option>";
                }
                $ret_val .= "</select>";
                return $ret_val;
            }
        }
    }

    function get_treatment_outcome() {
        if (func_num_args()>0) {
            $arg_list = func_get_args();
            $outcome_id = $arg_list[0];
        }
        $sql = "select outcome_name from m_lib_ntp_treatment_outcome where outcome_id = '$outcome_id'";
        if ($result = mysql_query($sql)) {
            if (mysql_num_rows($result)) {
                list($name) = mysql_fetch_array($result);
                return $name;
            }
        }
    }

    function form_outcome() {
    //
    // called by _ntp_outcomes()
    //
        if (func_num_args()) {
            $arg_list = func_get_args();
            $menu_id = $arg_list[0];
            $post_vars = $arg_list[1];
            $get_vars = $arg_list[2];
            if ($get_vars["outcome_id"]) {
                $sql = "select outcome_id, outcome_name ".
                       "from m_lib_ntp_treatment_outcome where outcome_id = '".$get_vars["outcome_id"]."'";
                if ($result = mysql_query($sql)) {
                    if (mysql_num_rows($result)) {
                        $outcome = mysql_fetch_array($result);
                    }
                }
            }
        }
        print "<table width='400'>";
        print "<form action = '".$_SERVER["SELF"]."?page=LIBRARIES&menu_id=$menu_id' name='form_outcome' method='post'>";
        print "<tr valign='top'><td>";
        print "<span class='library'>".FTITLE_TREATMENT_OUTCOME_FORM."</span><br><br>";
        print "</td></tr>";
        print "<tr valign='top'><td>";
        print "<span class='boxtitle'>".LBL_OUTCOME_ID."</span><br> ";
        if ($get_vars["outcome_id"]) {
            $disable = "disabled";
            print "<input type='hidden' name='outcome_id' value='".$get_vars["outcome_id"]."'>";
        } else {
            $disable = "";
        }
        print "<input type='text' class='textbox' size='10' $disable maxlength='10' name='outcome_id' value='".($outcome["outcome_id"]?$outcome["outcome_id"]:$post_vars["outcome_id"])."' style='border: 1px solid #000000'><br>";
        print "</td></tr>";
        print "<tr valign='top'><td>";
        print "<span class='boxtitle'>".LBL_OUTCOME_NAME."</span><br> ";
        print "<input type='text' class='textbox' size='15' maxlength='25' name='outcome_name' value='".($outcome["outcome_name"]?$outcome["outcome_name"]:$post_vars["outcome_name"])."' style='border: 1px solid #000000'><br>";
        print "</td></tr>";
        print "<tr><td><br>";
        if ($get_vars["outcome_id"]) {
            print "<input type='hidden' name='outcome_id' value='".$get_vars["outcome_id"]."'>";
            if ($_SESSION["priv_update"]) {
                print "<input type='submit' value = 'Update Outcome' class='textbox' name='submitoutcome' style='border: 1px solid #000000'> ";
            }
            if ($_SESSION["priv_delete"]) {
                print "<input type='submit' value = 'Delete Outcome' class='textbox' name='submitoutcome' style='border: 1px solid #000000'> ";
            }
        } else {
            if ($_SESSION["priv_add"]) {
                print "<input type='submit' value = 'Add Outcome' class='textbox' name='submitoutcome' style='border: 1px solid #000000'> ";
            }
        }
        print "</td></tr>";
        print "</form>";
        print "</table><br>";

    }

    function process_outcome() {
        if (func_num_args()>0) {
            $arg_list = func_get_args();
            $menu_id = $arg_list[0];
            $post_vars = $arg_list[1];
            $get_vars = $arg_list[2];
            $validuser = $arg_list[3];
            $isadmin = $arg_list[4];
            //print_r($arg_list);
        }
        if ($post_vars["submitoutcome"]) {
            if ($post_vars["outcome_id"] && $post_vars["outcome_name"]) {
                switch($post_vars["submitoutcome"]) {
                case "Add Outcome":
                    $sql = "insert into m_lib_ntp_treatment_outcome (outcome_id, outcome_name) ".
                           "values ('".strtoupper($post_vars["outcome_id"])."', '".$post_vars["outcome_name"]."')";
                    if ($result = mysql_query($sql)) {
                        header("location: ".$_SERVER["PHP_SELF"]."?page=".$get_vars["page"]."&menu_id=$menu_id");
                    }
                    break;
                case "Update Outcome":
                    $sql = "update m_lib_ntp_treatment_outcome set ".
                           "outcome_name = '".$post_vars["outcome_name"]."' ".
                           "where outcome_id = '".$post_vars["outcome_id"]."'";
                    if ($result = mysql_query($sql)) {
                        header("location: ".$_SERVER["PHP_SELF"]."?page=".$get_vars["page"]."&menu_id=$menu_id");
                    }
                    break;
                case "Delete Outcome":
                    if (module::confirm_delete($menu_id, $post_vars, $get_vars)) {
                        $sql = "delete from m_lib_ntp_treatment_outcome where outcome_id = '".$post_vars["outcome_id"]."'";
                        if ($result = mysql_query($sql)) {
                            header("location: ".$_SERVER["PHP_SELF"]."?page=".$get_vars["page"]."&menu_id=$menu_id");
                        }
                    } else {
                        if ($post_vars["confirm_delete"]=="No") {
                            header("location: ".$_SERVER["PHP_SELF"]."?page=".$get_vars["page"]."&menu_id=".$get_vars["menu_id"]);
                        }
                    }
                    break;
                }
            }
        }
    }

    function display_outcome() {
        if (func_num_args()) {
            $arg_list = func_get_args();
            $menu_id = $arg_list[0];
            $post_vars = $arg_list[1];
            $get_vars = $arg_list[2];
            $validuser = $arg_list[3];
            $isadmin = $arg_list[4];
        }
        print "<table width='400'>";
        print "<tr valign='top'><td colspan='3'>";
        print "<span class='library'>".FTITLE_NTP_TREATMENT_OUTCOME."</span><br>";
        print "</td></tr>";
        print "<tr valign='top'><td><b>".THEAD_ID."</b></td><td><b>".THEAD_NAME."</b></td></tr>";
        $sql = "select outcome_id, outcome_name from m_lib_ntp_treatment_outcome order by outcome_name";
        if ($result = mysql_query($sql)) {
            if (mysql_num_rows($result)) {
                while (list($id, $name) = mysql_fetch_array($result)) {
                    print "<tr valign='top'><td>$id</td><td><a href='".$_SERVER["SELF"]."?page=".$get_vars["page"]."&menu_id=$menu_id&outcome_id=$id'>$name</a></td></tr>";
                }
            }
        }
        print "</table><br>";
    }

    function _ntp_partners() {
    //
    // main submodule for treatment partners
    // calls form_partner()
    //       display_partner()
    //       process_partner()
    //
        // always check dependencies
        if ($exitinfo = $this->missing_dependencies('ntp')) {
            return print($exitinfo);
        }
        if (func_num_args()>0) {
            $arg_list = func_get_args();
            $menu_id = $arg_list[0];
            $post_vars = $arg_list[1];
            $get_vars = $arg_list[2];
            $validuser = $arg_list[3];
            $isadmin = $arg_list[4];
            //print_r($arg_list);
        }
        $n = new ntp;
        if ($post_vars["submitpartner"]) {
            $n->process_partner($menu_id, $post_vars, $get_vars);
        }
        $n->display_partner($menu_id, $post_vars, $get_vars);
        $n->form_partner($menu_id, $post_vars, $get_vars);
    }

    function show_treatment_partners() {
        if (func_num_args()>0) {
            $arg_list = func_get_args();
            $partner_id = $arg_list[0];
        }
        $sql = "select partner_id, partner_name from m_lib_ntp_treatment_partner order by partner_name";
        if ($result = mysql_query($sql)) {
            if (mysql_num_rows($result)) {
                $ret_val .= "<select name='treatment_partner' class='textbox'>";
                $ret_val .= "<option value=''>Select Partner</option>";
                while (list($id, $name) = mysql_fetch_array($result)) {
                    $ret_val .= "<option value='$id'>$name</option>";
                }
                $ret_val .= "</select>";
                return $ret_val;
            }
        }
    }

    function form_partner() {
    //
    // called by _ntp_partners()
    //
        if (func_num_args()) {
            $arg_list = func_get_args();
            $menu_id = $arg_list[0];
            $post_vars = $arg_list[1];
            $get_vars = $arg_list[2];
            if ($get_vars["partner_id"]) {
                $sql = "select partner_id, partner_name ".
                       "from m_lib_ntp_treatment_partner where partner_id = '".$get_vars["partner_id"]."'";
                if ($result = mysql_query($sql)) {
                    if (mysql_num_rows($result)) {
                        $partner = mysql_fetch_array($result);
                    }
                }
            }
        }
        print "<table width='400'>";
        print "<form action = '".$_SERVER["SELF"]."?page=LIBRARIES&menu_id=$menu_id' name='form_partner' method='post'>";
        print "<tr valign='top'><td>";
        print "<span class='library'>".FTITLE_TREATMENT_PARTNER_FORM."</span><br><br>";
        print "</td></tr>";
        print "<tr valign='top'><td>";
        print "<span class='boxtitle'>".LBL_PARTNER_ID."</span><br> ";
        if ($get_vars["partner_id"]) {
            $disable = "disabled";
            print "<input type='hidden' name='partner_id' value='".$get_vars["partner_id"]."'>";
        } else {
            $disable = "";
        }
        print "<input type='text' class='textbox' size='10' $disable maxlength='10' name='partner_id' value='".($partner["partner_id"]?$partner["partner_id"]:$post_vars["partner_id"])."' style='border: 1px solid #000000'><br>";
        print "</td></tr>";
        print "<tr valign='top'><td>";
        print "<span class='boxtitle'>".LBL_PARTNER_NAME."</span><br> ";
        print "<input type='text' class='textbox' size='15' maxlength='25' name='partner_name' value='".($partner["partner_name"]?$partner["partner_name"]:$post_vars["partner_name"])."' style='border: 1px solid #000000'><br>";
        print "</td></tr>";
        print "<tr><td><br>";
        if ($get_vars["partner_id"]) {
            print "<input type='hidden' name='partner_id' value='".$get_vars["partner_id"]."'>";
            if ($_SESSION["priv_update"]) {
                print "<input type='submit' value = 'Update Partner' class='textbox' name='submitpartner' style='border: 1px solid #000000'> ";
            }
            if ($_SESSION["priv_delete"]) {
                print "<input type='submit' value = 'Delete Partner' class='textbox' name='submitpartner' style='border: 1px solid #000000'> ";
            }
        } else {
            if ($_SESSION["priv_add"]) {
                print "<input type='submit' value = 'Add Partner' class='textbox' name='submitpartner' style='border: 1px solid #000000'> ";
            }
        }
        print "</td></tr>";
        print "</form>";
        print "</table><br>";

    }

    function process_partner() {
        if (func_num_args()>0) {
            $arg_list = func_get_args();
            $menu_id = $arg_list[0];
            $post_vars = $arg_list[1];
            $get_vars = $arg_list[2];
            $validuser = $arg_list[3];
            $isadmin = $arg_list[4];
            //print_r($arg_list);
        }
        if ($post_vars["submitpartner"]) {
            if ($post_vars["partner_id"] && $post_vars["partner_name"]) {
                switch($post_vars["submitpartner"]) {
                case "Add Partner":
                    $sql = "insert into m_lib_ntp_treatment_partner (partner_id, partner_name) ".
                           "values ('".strtoupper($post_vars["partner_id"])."', '".$post_vars["partner_name"]."')";
                    if ($result = mysql_query($sql)) {
                        header("location: ".$_SERVER["PHP_SELF"]."?page=".$get_vars["page"]."&menu_id=$menu_id");
                    }
                    break;
                case "Update Partner":
                    $sql = "update m_lib_ntp_treatment_partner set ".
                           "partner_name = '".$post_vars["partner_name"]."' ".
                           "where partner_id = '".$post_vars["partner_id"]."'";
                    if ($result = mysql_query($sql)) {
                        header("location: ".$_SERVER["PHP_SELF"]."?page=".$get_vars["page"]."&menu_id=$menu_id");
                    }
                    break;
                case "Delete Partner":
                    if (module::confirm_delete($menu_id, $post_vars, $get_vars)) {
                        $sql = "delete from m_lib_ntp_treatment_partner where partner_id = '".$post_vars["partner_id"]."'";
                        if ($result = mysql_query($sql)) {
                            header("location: ".$_SERVER["PHP_SELF"]."?page=".$get_vars["page"]."&menu_id=$menu_id");
                        }
                    } else {
                        if ($post_vars["confirm_delete"]=="No") {
                            header("location: ".$_SERVER["PHP_SELF"]."?page=".$get_vars["page"]."&menu_id=".$get_vars["menu_id"]);
                        }
                    }
                    break;
                }
            }
        }
    }

    function display_partner() {
        if (func_num_args()) {
            $arg_list = func_get_args();
            $menu_id = $arg_list[0];
            $post_vars = $arg_list[1];
            $get_vars = $arg_list[2];
            $validuser = $arg_list[3];
            $isadmin = $arg_list[4];
        }
        print "<table width='400'>";
        print "<tr valign='top'><td colspan='3'>";
        print "<span class='library'>".FTITLE_NTP_TREATMENT_PARTNER."</span><br>";
        print "</td></tr>";
        print "<tr valign='top'><td><b>".THEAD_ID."</b></td><td><b>".THEAD_NAME."</b></td></tr>";
        $sql = "select partner_id, partner_name from m_lib_ntp_treatment_partner order by partner_name";
        if ($result = mysql_query($sql)) {
            if (mysql_num_rows($result)) {
                while (list($id, $name) = mysql_fetch_array($result)) {
                    print "<tr valign='top'><td>$id</td><td><a href='".$_SERVER["SELF"]."?page=".$get_vars["page"]."&menu_id=$menu_id&partner_id=$id'>$name</a></td></tr>";
                }
            }
        }
        print "</table><br>";
    }

// end of class
}
?>
